/**
 *   Copyright 2008 Alexey Vishentsev
 *
 *   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

package com.gwtspreadsheetinput.jsf.component;

import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.convert.Converter;
import javax.faces.event.MethodExpressionValueChangeListener;
import javax.faces.validator.MethodExpressionValidator;
import javax.faces.webapp.UIComponentELTag;
import javax.servlet.jsp.JspException;

public class SpreadSheetColumnTag extends UIComponentELTag{
	private ValueExpression title;
	private int width;
	private ValueExpression converter;
	private ValueExpression converterMessage;
	private ValueExpression immediate;
	private ValueExpression required;
	private ValueExpression requiredMessage;
	private MethodExpression validator;
	private ValueExpression validatorMessage;
	private ValueExpression value;
	private MethodExpression valueChangeListener;
	
	public SpreadSheetColumnTag() {
		super();
	}
	
	@Override
	public String getComponentType() {
		return "com.gwtspreadsheetinput.jsf.SpreadSheetColumn";
	}

	@Override
	public String getRendererType() {
		return null;
	}
	
	@Override
	public int doStartTag() throws JspException {
		return super.doStartTag();
	}
	
	public void setTitle(ValueExpression title) {
		this.title = title;
	}
	
	public void setWidth(int width) {
		this.width = width;
	}
	
	public void setConverter(ValueExpression converter) {
		this.converter = converter;
	}

	public void setConverterMessage(ValueExpression converterMessage) {
		this.converterMessage = converterMessage;
	}

	public void setImmediate(ValueExpression immediate) {
		this.immediate = immediate;
	}

	public void setRequired(ValueExpression required) {
		this.required = required;
	}

	public void setRequiredMessage(ValueExpression requiredMessage) {
		this.requiredMessage = requiredMessage;
	}

	public void setValidator(MethodExpression validator) {
		this.validator = validator;
	}

	public void setValidatorMessage(ValueExpression validatorMessage) {
		this.validatorMessage = validatorMessage;
	}

	public void setValue(ValueExpression value) {
		this.value = value;
	}

	public void setValueChangeListener(MethodExpression valueChangeListener) {
		this.valueChangeListener = valueChangeListener;
	}

	protected void setProperties(UIComponent component) {
		super.setProperties(component);
		SpreadSheetColumn col = (SpreadSheetColumn) component;
		if (title.isLiteralText()){
			col.setTitle(title.getExpressionString());
		} else {
			col.setValueExpression("title", title);
		}
		col.setWidth(width);
		if (converter != null) {
			if (converter.isLiteralText()) {
				Converter c = getFacesContext().getApplication()
					.createConverter(converter.getExpressionString());
				col.setConverter(c);
			} else {
				col.setValueExpression("converter", converter);
			}
		}
		if (converterMessage != null) {
			col.setValueExpression("converterMessage", converter);
		}
		
		if (immediate != null) {
            col.setValueExpression("immediate", immediate);
        }

        if (required != null) {
            col.setValueExpression("required", required);
        }

        if (requiredMessage != null) {
            col.setValueExpression("requiredMessage", requiredMessage);
        }

        if (validator != null) {
            col.addValidator(new MethodExpressionValidator(validator));
        }
        if (validatorMessage != null) {
            col.setValueExpression("validatorMessage", validatorMessage);
        }

        if (value != null) {
            col.setValueExpression("value", value);
        }

        if (valueChangeListener != null) {
            col.addValueChangeListener(new MethodExpressionValueChangeListener(valueChangeListener));
        }
	}
	
	@Override
	public void release() {
		super.release();
		width = -1;
		title = null;
	}
	
}
